package com.cpx.service.impl;

import com.cpx.dto.MenuDto;
import com.cpx.dto.ShiroAuthoDto;
import com.cpx.mapper.EmployeeMapper;
import com.cpx.po.Employee;
import com.cpx.po.Permission;
import com.cpx.po.Role;
import com.cpx.service.EmployeeService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Auther: PanBin
 * @Date: 2018-9-5 14:45
 * @Description:
 * @Version: 1.0
 */
@RestController
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public Employee findEmployeeByUsername(String username) {
        Employee employee = employeeMapper.findEmployeeByUsername(username);
        return employee;
    }

    @Override
    public ShiroAuthoDto getAuthcListByEmployeeId(Integer id) {
        List<Permission> permissionList = employeeMapper.getPermissionListByEmployeeId(id);
        List<Role> roleList = employeeMapper.getRoleListByEmployeeId(id);
        ShiroAuthoDto shiroAuthoDto = new ShiroAuthoDto();
        shiroAuthoDto.setPermissionList(permissionList);
        shiroAuthoDto.setRoleList(roleList);
        return shiroAuthoDto;
    }

    @Override
    public List<MenuDto> getMenuByEmployeeId(Integer employeeId) {
        List<Permission> permissionList = employeeMapper.getPermissionListByEmployeeId(employeeId);
        List<MenuDto> menuList = null;//一级菜单
        if (permissionList != null && !permissionList.isEmpty()) {
            menuList = new ArrayList<>();
            Iterator<Permission> iterator = permissionList.iterator();
            while (iterator.hasNext()) {
                Permission p = iterator.next();
                Integer parentId = p.getParentId();
                if (parentId.intValue() == 0) {
                    MenuDto _menu = new MenuDto();
                    BeanUtils.copyProperties(p,_menu,"menuList");
                    menuList.add(_menu);
                    iterator.remove();
                }
            }
        }
        List<MenuDto> twoMenuList = null;
        if (menuList != null && !menuList.isEmpty()) {
            //获取二级菜单
            for (MenuDto menuDto : menuList) {
                Integer id = menuDto.getId();
                if (permissionList != null && !permissionList.isEmpty()) {
                    twoMenuList = new ArrayList<>();
                    Iterator<Permission> iterator = permissionList.iterator();
                    while (iterator.hasNext()) {
                        Permission p = iterator.next();
                        Integer parentId = p.getParentId();
                        if (id.equals(parentId)) {
                            MenuDto _menu = new MenuDto();
                            BeanUtils.copyProperties(p,_menu,"menuList");
                            twoMenuList.add(_menu);
                            iterator.remove();
                        }
                    }
                    menuDto.setMenuList(twoMenuList);
                    //获取三级菜单
                    List<MenuDto> threeMenuList = null;
                    if (twoMenuList != null && !twoMenuList.isEmpty()) {
                        for (MenuDto twoMenuDto : twoMenuList) {
                            Integer twoId = twoMenuDto.getId();
                            if (permissionList != null && !permissionList.isEmpty()) {
                                threeMenuList = new ArrayList<>();
                                Iterator<Permission> twoIterator = permissionList.iterator();
                                while (twoIterator.hasNext()) {
                                    Permission p = twoIterator.next();
                                    Integer parentId = p.getParentId();
                                    if (twoId.equals(parentId)) {
                                        MenuDto _menu = new MenuDto();
                                        BeanUtils.copyProperties(p,_menu,"menuList");
                                        threeMenuList.add(_menu);
                                    }
                                }
                                twoMenuDto.setMenuList(threeMenuList);
                            }
                        }
                    }
                }
            }
        }
        return menuList;
    }
}
